package com.css.gatewaynacos.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author 郭辉
 * @className TODO
 * @description TODO
 * @date 2022/8/22 14:56
 * @company 海康威视
 * @since 1.0.0
 */
@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpRequest.Builder requestBuilder = request.mutate();
        requestBuilder.headers(httpHeaders -> httpHeaders.add("gateway", "guohui"));
        // 构建新的ServerWebExchange实例
        exchange.mutate().request(request).build();
        return chain.filter(exchange);
    }


    @Override
    public int getOrder() {
        return 1000;
    }
}
